# 中国纬度范围: 3.86°N - 53.55°N
# 中国经度范围: 73.66°E - 135.05°E

from numpy import arange
import requests
import json
import sys
import os
import time

import pandas as pd
import json
from sqlalchemy import create_engine

engine = create_engine("mysql+pymysql://root:root@127.0.0.1:3306/yima")  # 数据库接口

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

if __name__ == "__main__":
    df = None
    data = {
        "lat": 0,
        "lon": 0,
        "pageNum": 1,
        "pageSize": 99999,
    }

    token = "F6279B2F5C3223D53AFA840603838F56CC2B8F496D78A76E22147EF9CD2F740A"

    headers = {
        "token": token,
    }

    url = "https://m.bolink.club/capp/api/park/getnearpark"

    rsp = requests.post(url, data=json.dumps(data), headers=headers)

    rspJson = rsp.json()

    total = len(rspJson["data"])

    print(f"len: {total}")

    index = 0

    df2 = pd.DataFrame(rspJson["data"])
    df2.drop(columns="dis", inplace=True)

    if df is None:
        df = df2
    else:
        df = pd.concat([df, df2])

    df.drop_duplicates(subset=["id"], keep="first", inplace=True)

    print(f"duplicates len:{len(df)}")

    time.sleep(1)

    df.to_sql("park", con=engine, index=False, if_exists="append")
